package com.cgsd.heyu.adapter;


import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.viewpager.widget.PagerAdapter;

import com.android.library.image.GlideUtil;
import com.github.chrisbanes.photoview.PhotoView;
import com.cgsd.heyu.entity.PhotoEntity;

import java.util.ArrayList;

/**
 * @author xiaoxing
 * Created by pengsheng-2 on 2018/5/15.
 */

public class PhotoPagerAdapter extends PagerAdapter {
    private ArrayList<PhotoEntity> photos;
    private Context mContext;

    public PhotoPagerAdapter(Context mContext, ArrayList<PhotoEntity> photos) {
        this.mContext =  mContext;
        this.photos = photos;
    }

    @Override
    public int getCount() {
        return photos.size();
    }

    @NonNull
    @Override
    public View instantiateItem(@NonNull ViewGroup container, int position) {
        PhotoView photoView = new PhotoView(mContext);
        if (photos.get(position).type == PhotoEntity.NETWORK){
            GlideUtil.loadRemoteImage(mContext,photos.get(position).imgUrl,photoView);
        }else {
            photoView.setImageResource(photos.get(position).imgResId);
        }
        container.addView(photoView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

        photoView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ((Activity)mContext).finish();
            }
        });
        return photoView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return view == object;
    }
}
